package com.hera.base.web.interceptor;

import java.lang.reflect.Method;
import java.util.Date;

import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.stereotype.Component;

import com.hera.base.domain.BaseDomain;
import com.hera.process.security.domain.User;
import com.vaadin.server.VaadinSession;

@Component("auditarInterceptor")
public class AuditoriaInterceptor implements MethodBeforeAdvice
{
	@Override
	public void before(Method method, Object[] args, Object target) throws Throwable {
		User user= (User)VaadinSession.getCurrent().getAttribute("USUARIO");
		if(user==null) return;
		for(Object obj:args){
			if(obj instanceof BaseDomain){
				BaseDomain baseDomain=(BaseDomain)obj;
				if (baseDomain.getUserCreated() == null || baseDomain.getDateCreated() == null) {
					baseDomain.setUserCreated(user.getUserLogin());
					baseDomain.setDateCreated(new Date());
				} else {
					baseDomain.setUserModified(user.getUserLogin());
					baseDomain.setDateModified(new Date());
				}
			}
		}
	}
	
	
}
